﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using SuperCalendar.Models;
using System.Text;
using System.Linq;
using System.Diagnostics;

namespace SuperCalendar.DataAccess
{
    public static class Events
    {

        private static Calendar calendar;
        private static String dataPath;
        private static bool isDirty;

        public static void Init()
        {
            if (calendar == null)
            {

                String username = ConfigurationSettings.AppSettings["UserName"];
                if (String.IsNullOrEmpty(username))
                    throw new ApplicationException("Configuration error, missing username");
                dataPath = Path.Combine(username, "data.xml");
                DirectoryInfo d = new DirectoryInfo(username);
                FileInfo f = new FileInfo(dataPath);
                if (!d.Exists)
                {
                    d.Create();
                }
                if (!f.Exists)
                {
                    calendar = new Calendar();
                    isDirty = true;
                    Save();
                }
                else
                {
                    XmlSerializer ser = new XmlSerializer(typeof(Calendar));
                    using (FileStream fs = new FileStream(dataPath, FileMode.OpenOrCreate))
                    {
                        calendar = (Calendar)ser.Deserialize(fs);
                    }
                }
            }
        }

        public static void Insert(Event even)
        {
            Debug.Assert(even != null);
            Init();
            even.IsNew = true;
            even.Published = DateTime.Now;
            even.Updated = DateTime.Now;
            calendar.Events.Add(even);
            isDirty = true;
        }

        public static void InsertRange(IEnumerable<Event> events)
        {

            Debug.Assert(events != null);
            Init();
            GetAllEvents().AddRange(events);
            isDirty = true;
        }

        public static List<Event> GetAllEvents()
        {
            Init();
            return calendar.Events;
        }

        public static void Delete(Event e)
        {
            Init();
            e.IsDeleted = true;
            isDirty = true;
        }

        public static void Update(Event e)
        {
            Init();
            e.Updated = DateTime.Now;
            int index = calendar.Events.IndexOf(e);
            if (index < 0)
                throw new ApplicationException("Update event error, the event dones't exists in local");

            calendar.Events[index] = e;
            isDirty = true;
        }
        public static void Save()
        {
            Init();
            if (isDirty)
            {
                XmlSerializer ser = new XmlSerializer(typeof(Calendar));
                using (FileStream fs = new FileStream(dataPath, FileMode.OpenOrCreate))
                {
                    ser.Serialize(fs, calendar);
                }

                isDirty = false;
            }
        }
    }
}
